package com.easy.service.impl;

import com.easy.bean.PurchaseRequest;
import com.easy.mapper.PurchaseRequestDao;
import com.easy.service.PurchaseRequestService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseRequestServiceImpl implements PurchaseRequestService {

    @Autowired
    PurchaseRequestDao purchaseRequestDao;

    @Override
    public PurchaseRequest add(PurchaseRequest request) {
        // 验证必填字段是否为空
        if (request.getMaterialId() == null) {
            throw new IllegalArgumentException("物料ID不能为空");
        }
        
        if (request.getQuantity() == null || request.getQuantity() <= 0) {
            throw new IllegalArgumentException("数量必须大于0");
        }
        
        if (request.getRequesterId() == null) {
            throw new IllegalArgumentException("申请人ID不能为空");
        }
        
        if (request.getDepartmentId() == null) {
            throw new IllegalArgumentException("部门ID不能为空");
        }
        
        purchaseRequestDao.add(request);
        return purchaseRequestDao.one(request.getId());
    }

    @Override
    public int del(Integer id) {
        return purchaseRequestDao.del(id);
    }

    @Override
    public PurchaseRequest edit(PurchaseRequest request) {
        purchaseRequestDao.edit(request);
        return purchaseRequestDao.one(request.getId());
    }

    @Override
    public List<PurchaseRequest> all(Integer materialId, Integer requesterId) {
        return purchaseRequestDao.all(materialId, requesterId);
    }

    @Override
    public PurchaseRequest one(Integer id) {
        return purchaseRequestDao.one(id);
    }

    @Override
    public void page(Page page, PurchaseRequest request) {
        int count = purchaseRequestDao.pageCount(request);
        List<PurchaseRequest> data = purchaseRequestDao.page(page, request);
        page.setCount(count);
        page.setData(data);
    }
}